package com.example.HelloSpace.adapters;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager2.widget.ViewPager2;

import com.bumptech.glide.Glide;
import com.example.HelloSpace.R;
import com.example.HelloSpace.utils.MyApp;
import com.google.android.material.imageview.ShapeableImageView;
import com.google.android.material.shape.ShapeAppearanceModel;

import java.util.ArrayList;
import java.util.List;

public class CarouselViewAdapter extends RecyclerView.Adapter<CarouselViewAdapter.CarouselViewHolder>{
    private List<String> imageUrls;
    private ViewPager2 viewPager2;

    public CarouselViewAdapter(List<String> imageUrls, ViewPager2 viewPager2) {
        this.imageUrls = new ArrayList<String>(imageUrls);
        this.imageUrls.add(0, imageUrls.get(imageUrls.size()-1));
        this.imageUrls.add(imageUrls.get(0));
        this.viewPager2 = viewPager2;
        setupInfiniteScroll();
    }
    private void setupInfiniteScroll() {
        viewPager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
//
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                super.onPageScrollStateChanged(state);
                if(state==ViewPager2.SCROLL_STATE_IDLE){
                    if (viewPager2.getCurrentItem()== getItemCount()-1) {
                        viewPager2.post(() -> viewPager2.setCurrentItem(1, false)); // 使用动画效果
                    }
                    //当接近第一个项目时，提前跳转到最后一个项目
                    else if (viewPager2.getCurrentItem() == 0) {
                        viewPager2.post(() -> viewPager2.setCurrentItem(getItemCount() - 2, false)); // 使用动画效果
                    }
                }
            }
        });
    }

    @NonNull
    @Override
    public CarouselViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_carousel_image, parent, false);
        return new CarouselViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull CarouselViewHolder holder, int position) {

        String imageUrl = imageUrls.get(position);
        ShapeableImageView imageView = holder.imageView;
        String url= MyApp.getRESOURCE_URL();
        // 设置圆角或形状（例如：圆角矩形）
        ShapeAppearanceModel shapeAppearanceModel = new ShapeAppearanceModel()
                .toBuilder()
                .setAllCornerSizes(40) // 设置所有角的半径为16dp
                .build();
        imageView.setShapeAppearanceModel(shapeAppearanceModel);

        Glide.with(imageView.getContext())
                .load(url+imageUrl)
                .into(imageView);
    }

    @Override
    public int getItemCount() {
        return imageUrls.size();
    }

    static class CarouselViewHolder extends RecyclerView.ViewHolder {

        ShapeableImageView imageView;

        CarouselViewHolder(@NonNull View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.imageView);
        }
    }
}
